<?php

namespace Common\Model;

use Think\Model;
use User\Api\UserApi;

/**
 * 粉丝操作
 */
class FollowModel extends Model {
    function init_follow($openid) {
        if (empty ( $openid ) || $openid == - 1)
            return false;
        
        $data ['token'] = get_token ();
        $data ['openid'] = $openid;
        $winfo = getWeixinUserInfo ($openid, $data ['token']);
        $info = $this->where ( $data )->find ();
        
        if ($info) {
            $save ['mTime'] = date('Y-m-d H:i:s', time ());
            $save ['status'] = 1;
            $save ['nickname'] = $winfo ['nickname'];
            $save ['sex'] = $winfo ['sex'];
            $save ['city'] = $winfo ['city'];
            $save ['country'] = $winfo ['country'];
            $save ['province'] = $winfo ['province'];
            $save ['language'] = $winfo ['language'];
            $save ['headimgurl'] = $winfo ['headimgurl'];
            $save ['unionid'] = $winfo ['unionid'];
            $save ['remark'] = $winfo ['remark'];
            $save ['group_id'] = $winfo ['groupid'];
            $res = $this->where ( $data )->save ( $save );
        } else {
            $data ['subscribe_time'] = date('Y-m-d H:i:s', $winfo ['subscribe_time']);
            $data ['nickname'] = $winfo ['nickname'];
            $data ['sex'] = $winfo ['sex'];
            $data ['city'] = $winfo ['city'];
            $data ['country'] = $winfo ['country'];
            $data ['province'] = $winfo ['province'];
            $data ['language'] = $winfo ['language'];
            $data ['headimgurl'] = $winfo ['headimgurl'];
            $data ['unionid'] = $winfo ['unionid'];
            $data ['remark'] = $winfo ['remark'];
            $data ['group_id'] = $winfo ['groupid'];
            $uid = $this->get_uid_by_ucenter ( $data ['openid'], $data ['token'] );
            if ($uid > 0) {
                $data ['id'] = $uid;
                $res = $this->add ( $data );
            }
            
            $info = $data;
        }
        return $info;
    }
    // 自动初始化微信用户
    function get_uid_by_ucenter($openid, $token) {
        static $_email_int = 0;
        $info ['openid'] = $openid;
        $info ['token'] = $token;
        $res = M ( 'ucenter_member' )->where ( $info )->find ();
        
        if ($res)
            return $res ['id'];
        
        $email = time () . rand ( 01, 99 ) . $_email_int . '@weiphp.cn';
        $nickname = uniqid () . rand ( 01, 99 ) . $_email_int;
        
        /* 调用注册接口注册用户 */
        $User = new UserApi ();
        $uid = $User->register ( $nickname, '123456', $email, '', $openid, $token );
        
        $_email_int += 1;
        
        return $uid;
    }
    
    /**
     * 获取粉丝全部信息
     */
    public function getFollowInfo($id) {
        static $_followInfo;
        if (isset ( $_followInfo [$id] )) {
            return $_followInfo [$id];
        }
        
        $_followInfo [$id] = $this->find ( $id );
        return $_followInfo [$id];
    }
}
?>
